﻿/**
 * SlotDuino
 * 
 * Copyright (c) 2011/2012 Thiago Hummel Moreira, Ricardo Mizuuti, Rafael Mufato, Bruno Sakai, Andre Turina
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SlotDuino.DAL;
using System.Linq;
using SlotDuino.UI;
using SlotDuino.Reports;
using SlotDuino.Util;

namespace SlotDuino
{
	public partial class ChampionshipList
	{


        #region Dependency properties
        public static readonly DependencyProperty ChampionshipProperty = DependencyProperty.Register("Championship", typeof(Championship), typeof(ChampionshipList));
        #endregion


        #region Private attributes
        private EntityContainer _context;
        private CollectionViewSource _viewSource;
        private BindingListCollectionView _view;
        #endregion

        #region Public properties
        public Championship Championship
        {
            get { return (Championship)GetValue(ChampionshipProperty); }
            set { SetValue(ChampionshipProperty, value); }
        }
        #endregion



		public ChampionshipList()
		{
			this.InitializeComponent();
		}

        private void NavigateTo(string page)
        {
            NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo("UI/Pages/ChampionshipCreate.xaml");
        }

        private IQueryable<Championship> GetAllChampionships(EntityContainer context, string search = null)
        {
            if (null != search)
            {
                return from p in context.Championships where p.Name.Contains(search) select p;
            }
            else
            {
                return from p in context.Championships select p;
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //Create the entity context
            _context = new EntityContainer();
            //Get a reference to viewSource (defined on the XAML)
            _viewSource = ((CollectionViewSource)(FindResource("championshipViewSource")));

            RefreshData();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            RefreshData(txtSearch.Text);
        }


        private void RefreshData(string search = null)
        {
            //Refresh Data
            _viewSource.Source = GetAllChampionships(_context, search);
            _view = (BindingListCollectionView)_viewSource.View;
        }

        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            int raceid = ((Championship)dgChampList.SelectedItem).Id;
            Window window = new ReportViewer(new ChampionshipRaces(), raceid);
            window.Show();
        }

        private void btnResume_Click(object sender, RoutedEventArgs e)
        {
            //@TODO: Improve this
            Application.Current.Properties["raceDashboardMode"] = "Championship";
            Application.Current.Properties["currentChampionshipId"] = ((Championship)dgChampList.SelectedItem).Id;
            NavigateTo("UI/Pages/RaceDashboard.xaml");
        }

        private void dgChampList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((Championship)dgChampList.SelectedItem).State == 3 || 
                    ((Championship)dgChampList.SelectedItem).State == 4)
            {
                btnResume.IsEnabled = false;
            }
            else
            {
                btnResume.IsEnabled = true;
            }

             if (((Championship)dgChampList.SelectedItem).State == 0 )
             {
                 btnDelete.IsEnabled = true;
                 btnReport.IsEnabled = false;
             }
             else
             {
                 btnDelete.IsEnabled = false;
                 btnReport.IsEnabled = true;
             }

       }

	}
}